﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace RoMBot
{
    public enum LogType
    {
        Normal,
        Debug,
        Warning,
        Error
    }

    public class LogEventArgs : EventArgs
    {
        public LogEventArgs( string text, LogType type, Color color )
        {
            Text = text;
            Type = type;
            Color = color;
        }
        public string Text { get; set; }
        public LogType Type { get; set; }

        public Color Color { get; set; }
    }

    public static class Logger
    {
        private static readonly Queue<LogItem> logItems = new Queue<LogItem>();
        private static readonly bool enabled = true;
        public static event LogArrived LogArrived;
        private const string LogFormat = "{0}{1}";

        private static void OnLogArrived( string text, LogType type, Color color )
        {
            if ( !enabled || string.IsNullOrWhiteSpace( text ) )
                return;
            System.Diagnostics.Debug.WriteLine( string.Format( LogFormat, string.Format( type != LogType.Normal ? "[{0}] " : "", type.ToString().ToUpper() ), text ) );
            var handler = LogArrived;
            if ( handler != null )
            {
                handler( new LogEventArgs( string.Format( LogFormat, string.Format( type != LogType.Normal ? "[{0}] " : "", type.ToString().ToUpper() ), NormalizeNewLine( text ) ), type, color ) );
            }
        }

        public static Queue<LogItem> LogItems
        {
            get
            {
                return logItems;
            }
        }

        static Logger()
        {
        }

        public static void Debug( string text )
        {
            OnLogArrived( text, LogType.Debug, Color.White );
        }

        public static void Warning( string text )
        {
            OnLogArrived( text, LogType.Warning, Color.Yellow );
        }

        public static void Error( string text )
        {
            OnLogArrived( text, LogType.Error, Color.Red );
        }

        public static void Log( string text )
        {
            OnLogArrived( text, LogType.Normal, Color.White );
        }

        public static void Log( string text, Color color )
        {
            Log( text, LogType.Normal, color );
        }

        public static void Log( string text, LogType type )
        {
            Log( text, type, Color.White );
        }

        public static void Log( string text, LogType type, Color color )
        {
            OnLogArrived( text, type, color );
        }

        private static string NormalizeNewLine( string text )
        {
            return text + "\u2028";
        }
    }

    public delegate void LogArrived( LogEventArgs args );
}